from sqlalchemy.orm import Session
from fastapi import Depends, HTTPException, UploadFile
from apps.blueprints.user import models, schemas
from apps.extend.security import get_password_hash, verify_password
import time
import config
import os


def get_role_by_id(db: Session, role_id: int):
    role = db.query(models.Role).filter(models.Role.id == role_id).first()
    return role


def get_user_by_id(db: Session, user_id: int):
    user = db.query(models.User).filter(models.User.id == user_id).first()
    if user is None:
        raise HTTPException(status_code=404, detail="User not found")
    role = get_role_by_id(db, user.role_id)
    res = user.to_dict()
    res.update({"role": role.to_dict()})
    return res


def get_user_by_contact_number(db: Session, contact_number: str):
    return db.query(models.User).filter(models.User.contact_number == contact_number).first()


def get_users(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.User).offset(skip).limit(limit).all()


def create_user(db: Session, user: schemas.UserCreate):
    hashed_password = get_password_hash(user.hashed_password)
    args = user.dict()
    args.update({'hashed_password': hashed_password})
    db_user = models.User(**args)
    db.add(db_user)
    db.commit()
    db.refresh(db_user)
    return db_user


def get_items(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.Item).offset(skip).limit(limit).all()


def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int):
    db_item = models.Item(**item.dict(), owner_id=user_id)
    db.add(db_item)
    db.commit()
    db.refresh(db_item)
    return db_item


# 验证账号和密码
def authenticate_user(db: Session,  username: str, password: str):
    user = get_user_by_contact_number(db, contact_number=username)
    if not user:
        return False
    if not verify_password(password, user.hashed_password):
        return False
    return user


def get_roles(db: Session, skip: int = 0, limit: int = 100):
    roles = db.query(models.Role).offset(skip).limit(limit).all()
    return roles


# 编辑用户资料
def update_user(db: Session, user_id: int, update_user: schemas.UpdateUser):
    get_user_by_id(db, user_id)  # 验证用户是否存在
    db.query(models.User).filter(models.User.id == user_id).update(update_user.dict())
    db.commit()


# 修改密码
def updata_password(db: Session, user_id: int, PasswordUser: schemas.PasswordUser):
    get_user_by_id(db, user_id)  # 验证用户是否存在
    user = db.query(models.User).get(user_id)
    res = verify_password(PasswordUser.old_password, user.hashed_password)
    if res:
        user.hashed_password = get_password_hash(PasswordUser.new_password)
        db.commit()
        return True
    else:
        return False


# 用户上传头像
async def user_upload_file(db: Session, user_id: int, file_path: str):
    get_user_by_id(db, user_id)  # 验证用户是否存在
    try:
        user = db.query(models.User).get(user_id)
        user.photo = file_path
        db.commit()
    except:
        raise HTTPException(status_code=500, detail="上传失败")




